package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.mapper.EchartsMapper;
import cn.kgc.ssm.service.EchartsService;
import cn.kgc.ssm.util.ResponseResult;
import cn.kgc.ssm.vo.LineEchartsData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 课工场
 * @date 2023/10/30
 * @description
 */
@Service
public class EchartsServiceImpl implements EchartsService {

    @Resource
    private EchartsMapper echartsMapper;

    @Override
    public ResponseResult getLineData() {

        List<Map<String,Object>>  lineDatas =  echartsMapper.getLineData();

        // 保存所有的专业名称
        HashSet<Object> majors = new HashSet<>();
        // 保存近五年年份
        HashSet<Object> years = new HashSet<>();

        for (Map<String, Object> lineData : lineDatas) {
               majors.add(lineData.get("major"));
               years.add(lineData.get("year"));
        }

        // LineEchartsData    {name:java,type:line,data:[0,1,1,3,33]}
        ArrayList<Object> datas = new ArrayList<>();

        for (Object major : majors) {
            LineEchartsData lineEchartsData = new LineEchartsData();
            lineEchartsData.setName(major);
            // 存储主页每年的招生人数的集合  [1,1]
            ArrayList<Object> yearData = new ArrayList<>();

            for (Object year : years) {

                Boolean flag = false;

                for (Map<String, Object> lineData : lineDatas) {

                    if (major.equals(lineData.get("major"))&&year.equals(lineData.get("year"))){
                        yearData.add(lineData.get("total"));
                        flag = true;
                        break;
                    }
                }
                //  判定当前专业该年份有没有学生
                if (!flag){
                    yearData.add(0);
                }
            }

            lineEchartsData.setData(yearData);
            datas.add(lineEchartsData);
        }

        HashMap<String, Object> result = new HashMap<>();

        result.put("major",majors);
        result.put("year",years);
        result.put("data",datas);

        return ResponseResult.success(result,null);
    }
}
